package com.ruoyi.production.service.impl;

import com.ruoyi.base.domain.Product;
import com.ruoyi.base.mapper.ProductMapper;
import com.ruoyi.common.utils.SeqNumberUtils;
import com.ruoyi.production.domain.ProductionWorkOrder;
import com.ruoyi.production.domain.ProductionWorkOrderDetail;
import com.ruoyi.production.domain.vo.ProductionWorkOrderAndPlanVo;
import com.ruoyi.production.mapper.ProductionWorkOrderMapper;
import com.ruoyi.production.service.IProductionInWarehouseService;
import com.ruoyi.stock.domain.WarehouseIn;
import com.ruoyi.stock.domain.WarehouseInInfo;
import com.ruoyi.stock.mapper.WarehouseInInfoMapper;
import com.ruoyi.stock.mapper.WarehouseInMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ProductionInWarehouseServiceImpl implements IProductionInWarehouseService {

    @Autowired
    private ProductionWorkOrderMapper productionWorkOrderMapper;

    @Autowired
    private WarehouseInMapper warehouseInMapper;

    @Autowired
    private WarehouseInInfoMapper warehouseInInfoMapper;

    @Autowired
    private ProductMapper productMapper;

    @Transactional
    @Override
    public int addProductionInWarehouse(Long workOrderId) {
        ProductionWorkOrderAndPlanVo productionWorkOrderAndPlanVo = productionWorkOrderMapper.selectProductionWorkOrderVoById(workOrderId);
        WarehouseIn warehouseIn = new WarehouseIn(null, SeqNumberUtils.getSCRKNumber(),productionWorkOrderAndPlanVo.getProductionNo(),
                2L,0L,null,null,null);
        int i = warehouseInMapper.insertWarehouseIn(warehouseIn);
        for (ProductionWorkOrderDetail productionWorkOrderDetail : productionWorkOrderAndPlanVo.getProductionWorkOrderDetailList()) {
            Product product = productMapper.selectProductById(productionWorkOrderDetail.getProductionId());
            WarehouseInInfo warehouseInInfo = new WarehouseInInfo(null, warehouseIn.getId(), product.getProductNo(),
                    null, productionWorkOrderDetail.getProductionCount(),null,null);
            i+=warehouseInInfoMapper.insertWarehouseInInfo(warehouseInInfo);
        }
        ProductionWorkOrder productionWorkOrder = new ProductionWorkOrder();
        productionWorkOrder.setId(workOrderId);
        productionWorkOrder.setStatus(3);
        productionWorkOrderMapper.updateProductionWorkOrder(productionWorkOrder);
        return i;
    }
}
